/**
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */
package com.poltman.dscentral.dscentral.menuPolicies;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.ListModelList;

import com.poltman.dscentral.db.entity.pm.MainMenuGroupsEntity;
import com.poltman.dscentral.db.service.pm.MainMenuGroupsService;

/**
 * 
 * @author Zbigniew Ciok; z.ciok@poltman.com
 * @version $Revision: 1711 $
 * 
 */
public class ExistingGroupsVM extends MenuItemsModel {
	
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(ExistingGroupsVM.class);	
	
	List<MenuItemsModel> items;
	public List<MenuItemsModel> getItems() {
		return items;
	}
	public void setItems(List<MenuItemsModel> items) {
		this.items = items;
	}

	ListModelList<String> existingGroupsShortcuts = null;
	
	@SuppressWarnings("rawtypes")
	ListModelList existingGroupsItems = null;
	
	String selectedShortcut;
	
    @SuppressWarnings({ "unchecked", "rawtypes" })
	@Init
    public void init() throws Exception {
		if (existingGroupsShortcuts == null) {
			MainMenuGroupsService mainMenuGroupsService = (MainMenuGroupsService) SpringUtil.getBean("mainMenuGroupsService");
			
			List<String> shortcuts = (List<String>) mainMenuGroupsService.findAllShortcuts();
			//we need empty 0 item
			List<String> scuts = new ArrayList<String>();
			scuts.add("");
			for (String s : shortcuts) scuts.add(s);
			this.existingGroupsShortcuts = new ListModelList(scuts);
		}
    }

	public ListModelList<String> getExistingGroupsShortcuts() throws Exception {
		this.init();
		return existingGroupsShortcuts;
	}

	@SuppressWarnings("rawtypes")
	public ListModelList getExistingGroupsItems() throws Exception {
		
		MainMenuGroupsService mainMenuGroupsService = (MainMenuGroupsService) SpringUtil.getBean("mainMenuGroupsService");
		List<MainMenuGroupsEntity> groups = mainMenuGroupsService.findByShortcut(this.selectedShortcut);	
		
		items = new ArrayList<MenuItemsModel>();
		for (int i = 0; i < groups.size(); i++) {
			String sItem = groups.get(i).getMenuItem();
			MenuItemsModel item = new MenuItemsModel();
			item.setName(sItem);
			items.add(item);
		}		
		this.existingGroupsItems = new ListModelList<MenuItemsModel>(items);		
		return this.existingGroupsItems;
	}

	public void setSelectedShortcut(String selectedShortcut) {
		this.selectedShortcut = selectedShortcut;
	}
}
